package vmcs.customer.coinstrategy;

import java.util.ArrayList;
import java.util.List;

import vmcs.store.Coin;
import vmcs.store.StoreItem;

/**CompositeCoinValidationStrategy is composite class of ICoinValidationStrategy. Client will add all the strategy that he wants to use for coin validation.
 * 
 * @author Atul
 *
 */
public class CompositeCoinValidationStrategy implements ICoinValidationStrategy {
	private List<ICoinValidationStrategy> strategyList;
	
	public CompositeCoinValidationStrategy()
	{
		super();
		strategyList = new ArrayList<ICoinValidationStrategy>();
	}
	
	@Override
	public Coin validateCoin(StoreItem[] coins, Coin coin) {
		Coin validatedCoin=null;
		Coin tempValidatedCoin = null;
		for(ICoinValidationStrategy strategy : strategyList)
		{
			validatedCoin = strategy.validateCoin(coins, coin);
			if(validatedCoin==null)
			{
				return validatedCoin;
			}
			
			if (tempValidatedCoin != null){
				if (tempValidatedCoin.getValue() != validatedCoin.getValue()) return null;
			}
			
			tempValidatedCoin = validatedCoin;
			
		}
		return validatedCoin;
	}
	
	/**This method is used to add the CoinValidation Strategy to Composite Strategy.
	 * 
	 * @param strategy
	 */
	public void add(ICoinValidationStrategy strategy)
	{
		if(strategy!=null && !strategyList.contains(strategy))
		{
			strategyList.add(strategy);
		}
	}
	
	/**This method is used to remove the CoinValidation Strategy to Composite Strategy.
	 * 
	 * @param strategy
	 */
	public void remove(ICoinValidationStrategy strategy)
	{
		if(strategy!=null && strategyList.contains(strategy))
		{
			strategyList.remove(strategy);
		}
	}

}
